﻿namespace Postnuclear
  {
  /// <summary>
  /// The extension methods for the <see cref="WeakReference{TTarget}"/>.
  /// </summary>  
  public static partial class ExtensionMethods
    {
    #region Public

    // Methods

    /// <summary>
    /// Tries the get the target.
    /// </summary>
    /// <typeparam name="TTarget">The type of the target.</typeparam>
    /// <param name="reference">The weak reference.</param>
    /// <param name="target">The resulting target strong reference.</param>
    /// <returns>
    /// <c>true</c> is target is alive and it was obtained successfully; otherwise - <c>false</c>.
    /// </returns>
    public static bool TryGetTarget<TTarget>(this WeakReference<TTarget> reference, out TTarget target)
      where TTarget : class
      {
      target = reference.Target;
      return reference.IsAlive;
      }

    #endregion
    }
  }